/**
 * @file display.c
 * @author taylor peng
 * @brief 显示函数
 * @version 0.1
 * @date 2022-01-09
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include "display.h"
#include <stdbool.h>
#include <string.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C Disp(U8G2_R0,        /* reset=*/
                                         OLED_RESET_PIN, /* clock=*/
                                         OLED_CLK_PIN,   /* data=*/
                                         OLED_DATA_PIN);

static unsigned int DisplayFlashTick = 0;

/**
 * @brief 刷新tick计数
 * 
 */
void Display_RefreshTickSet(void)
{
    DisplayFlashTick++;
}

unsigned int Display_RefreshTickGet(void)
{
    return DisplayFlashTick;
}

/**
 * @brief 清屏函数
 * 
 */
void Display_Clear(void)
{
    Disp.clearBuffer();
}

/**
 * @brief 显示函数
 * 
 */
void Display(void)
{
    Disp.sendBuffer();
}

/**
 * @brief 像素点
 * 
 */
void Display_Pixel(unsigned short x_pos, unsigned short y_pos)
{
    Disp.drawPixel(x_pos, y_pos);
}

/**
 * @brief 显示UTF
 * 
 */
void Display_Utf(unsigned short x_pos, unsigned short y_pos, const char *str)
{
    Disp.drawUTF8(x_pos, y_pos, str);
}

/**
 * @brief 获取高度
 * 
 */
unsigned short Display_GetHeight(const char *str)
{
    return Disp.getUTF8Width(str);
}

/**
 * @brief 
 * 
 */
unsigned short Display_GetWidth(const char *str)
{
    return Disp.getUTF8Width(str);
}

/**
 * @brief 获取字符最大高度
 * 
 * @return signed short 
 */
signed short Display_GetMaxCharHeight(void)
{
    return Disp.getMaxCharHeight();
}

/**
 * @brief 设置颜色
 * 
 */
void Display_SetColor(unsigned short color)
{
    Disp.setDrawColor(color);
}

/**
 * @brief 获取颜色
 * 
 */
unsigned char Display_GetColor(void)
{
    return Disp.getDrawColor();
}

/**
 * @brief 实心方形
 * 
 */
void Display_Box(unsigned short x_pos, unsigned short y_pos, unsigned short w_pos, unsigned short h_pos)
{
    Disp.drawBox(x_pos, y_pos, w_pos, h_pos);
}

/**
 * @brief 实心圆弧形方形
 * 
 */
void Display_RBox(unsigned short x_pos, unsigned short y_pos, unsigned short w_pos, unsigned short h_pos, unsigned short radius)
{
    Disp.drawRBox(x_pos, y_pos, w_pos, h_pos, radius);
}

/**
 * @brief 空心方形
 * 
 */
void Display_Framework(unsigned short x_pos, unsigned short y_pos, unsigned short w_pos, unsigned short h_pos)
{
    Disp.drawFrame(x_pos, y_pos, w_pos, h_pos);
}

/**
 * @brief 垂直线
 * 
 */
void Display_VLine(unsigned short x_pos, unsigned short y_pos, unsigned short h_pos)
{
    Disp.drawVLine(x_pos, y_pos, h_pos);
}

/**
 * @brief 水平线
 * 
 */
void Display_HLine(unsigned short x_pos, unsigned short y_pos, unsigned short w_pos)
{
    Disp.drawHLine(x_pos, y_pos, w_pos);
}

/**
 * @brief 显示字符串
 * 
 */
void Display_String(unsigned short x_pos, unsigned short y_pos, const char *str)
{
    Disp.drawStr(x_pos, y_pos, str);
}

/**
 * @brief 显示光标
 * 
 */
void Display_Cursor(unsigned short x_pos, unsigned short y_pos)
{
    Disp.setCursor(x_pos, y_pos);
}

/**
 * @brief 显示打印
 * 
 */
void Display_Print(const char *str)
{
    Disp.print(str);
}

/**
 * @brief 设置显示字体
 * 
 */
void Display_Font(unsigned char font_type)
{
    switch (font_type)
    {
        case LOGISOSO26_TF:
            Disp.setFont(u8g2_font_logisoso26_tf);
        break;

        case WQY12_T_GB2312:
            Disp.setFont(u8g2_font_wqy12_t_gb2312);
        break;

        case LOGISOSO38_TR:
            Disp.setFont(u8g2_font_logisoso38_tr);
        break;
    }
}

/**
 * @brief 显示三角形
 * 
 */
void Display_Triangle(signed short x0_pos, signed short y0_pos, signed short x1_pos, signed short y1_pos, signed short x2_pos, signed short y2_pos)
{
    Disp.drawTriangle(x0_pos, y0_pos, x1_pos, y1_pos, x2_pos, y2_pos);
}

/**
 * @brief 调节电阻
 * 
 */
void Display_SendF(const char *fmt, unsigned char cmd)
{
    Disp.sendF(fmt, cmd);
}

/**
 * @brief 显示初始化
 * 
 */
void Display_Init(void)
{
    //初始化OLED
    Disp.begin();
    Disp.setBusClock(921600);
    Disp.enableUTF8Print();
    Disp.setFontDirection(0);
    Disp.setFontPosTop();
    Disp.setFont(u8g2_font_wqy12_t_gb2312);
    Disp.setDrawColor(1);
    Disp.setFontMode(1);
}
